<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/19 0019
 * Time: 下午 3:45
 */
namespace app\index\controller;
use app\common\Jwt;

class Base extends \think\Controller{
    public $user_id='';
    function __construct()
    {
        parent::__construct();
        header("Access-Control-Allow-Origin:*"); //*号表示所有域名都可以访问
        //header("Access-Control-Allow-Method:POST,GET");
        header('Access-Control-Allow-Methods:*');
        header('Access-Control-Allow-Headers:*');
        error_reporting(E_ALL^E_NOTICE);

    }

    public function  apiJosn($data=[],$code=200,$tip="成功返回"){

        header('Content-type: application/json');
        $tem_data['data']=$data;
        $tem_data['code']=$code;
        $tem_data['tip']=$tip;
        $arr=json_encode($tem_data);

        echo $arr;
        exit();

    }


    function aa(){

        echo "<pre>";
        $payload = array('username'=>'i am username','password'=>'i am password');//规则可以自定义,本人喜欢用账号和密码
        var_dump($payload);
        $jwt = new Jwt;
        $token = $jwt->getToken($payload);
        echo $token;

//对token进行验证签名
        $getPayload = $jwt->verifyToken($token);
        echo "<br><br>";
        var_dump($getPayload);
        echo "<br><br>";
//接下来
//开发中先判断$getPayload中是否存在自定义规则的指定字段，不存在失败
//然后去验证当前用户的username，password，token是否在user表中存在记录，不存在失败

    }

    function get_token($payload){
        $jwt=new Jwt;
        $token=$jwt->getToken($payload);
        return $token;
    }

    function get_info_by_token($token){
        $jwt=new Jwt;
        $getPayload=$jwt->verifyToken($token);
        return $getPayload;
        //var_dump($getPayload);
    }
}